#!/usr/bin/env python
import socket
import sys,os

def start_tunnel(str_remoteIP, str_port):
    tmp = os.popen("ps -ef")
    tmp2 = tmp.read()
    tmp.close()
    if tmp2.find(str_port) < 0:
        os.system("ssh -Nf -L {1}:127.0.0.1:{1} tester@{0}".format(str_remoteIP, str_port))
        os.system("sleep 2")


remoteIP, str_PORT = socket.gethostbyname('10.239.131.30'), '16180'
data = " ".join(sys.argv[1:])

start_tunnel(remoteIP, str_PORT)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    sock.connect(('127.0.0.1', int(str_PORT)))
    sock.sendall( str(sys.argv[1:])  )
    received = sock.recv(1<<20)
finally:
    sock.close

print "Sent:\n%s" % (data)
print "Received:\n%s" % (received)
exec received
